package com.zsrt.utils;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.Map;
import java.util.Properties;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

import javax.servlet.http.HttpServletResponse;

import com.google.gson.Gson;
import com.zsrt.common.costant.DriverConstant;


/**
 * 返回驱动信息给用户
 * @author 罗智壕-13424431416
 *
 * 2017年5月24日
 */
public class DBUtils {
	
	
	/*** 
     * 判断 String 是否int 
     *  
     * @param input 
     * @return 
     */  
    public static boolean isInteger(String input){  
        Matcher mer = Pattern.compile("^[0-9]+$").matcher(input);  
        return mer.find();  
    }  
	
	public static void toJson(Map<String, Object> map,HttpServletResponse response){
		Gson gson = new Gson();
		String json = gson.toJson(map);
		try {
			response.getWriter().write(json);
			response.getWriter().flush();
			response.getWriter().close();
		} catch (IOException e) {
			e.printStackTrace();
		}
	}
	
	
	/**
	 * 关闭资源
	 * @param con
	 * @param ps
	 */
	public static void closeResources(Connection con,PreparedStatement ps,ResultSet rs){
		if(null != rs){
			try {
				rs.close();
			} catch (SQLException e) {
				e.printStackTrace();
			}
		}
		if(null != con){
			try {
				con.close();
			} catch (SQLException e) {
				e.printStackTrace();
			}
		}
		if(null != ps){
			try {
				ps.close();
			} catch (SQLException e) {
				e.printStackTrace();
			}
		}
	}
	
	/**
	 * 关闭资源
	 * @param con
	 * @param ps
	 */
	public static void closeResources(Connection con,PreparedStatement ps){
		if(null != con){
			try {
				con.close();
			} catch (SQLException e) {
				e.printStackTrace();
			}
		}
		if(null != ps){
			try {
				ps.close();
			} catch (SQLException e) {
				e.printStackTrace();
			}
		}
	}
	
	/**
	 * 返回驱动类的信息方法
	 * @return
	 */
	public static Connection getMessage(){
		
		Properties prop =  new  Properties();    
        File file = new File(DriverConstant.PRO_FILE);    
        FileInputStream fis = null;
        Connection connection = null;
		try {
			fis = new FileInputStream(file);
			prop.load(fis);
	        String driver = prop.getProperty(DriverConstant.DRIVER);
	        String url = prop.getProperty(DriverConstant.URL);
	        String username = prop.getProperty(DriverConstant.USERNAME);
	        String password = prop.getProperty(DriverConstant.PASSWORD);
	        
	        //加载驱动
	        Class.forName(driver);
	        //返回一个链接
	        connection = DriverManager.getConnection(url, username, password);
		} catch (FileNotFoundException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		} catch (ClassNotFoundException e) {
			e.printStackTrace();
		} catch (SQLException e) {
			e.printStackTrace();
		}finally{
			if(null != fis){
				try {
					fis.close();
				} catch (IOException e) {
					e.printStackTrace();
				}
			}
		}
		
		return connection;
	}
}
